package com.esreport.Utils;

import java.io.UnsupportedEncodingException;

/**
 * @author xuweiwei
 * @date 2019/1/16
 */
public class HexStringUtil {

    public static String hexToString(String hexString, String encoding) throws UnsupportedEncodingException {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        StringBuilder result = new StringBuilder(hexString.length());
        byte[] d = new byte[hexString.length()/4+1];
        int start = 0;
        for (int i = 0; i < hexString.length(); i++) {
            char c = hexString.charAt(i);
            boolean mode16 = false;
            if (c == '\\') {
                String next = hexString.substring(i, i + 4);
                if (next.length() == 4 && (next.startsWith("\\X") || next.startsWith("\\x")) ) {
                    byte b = (byte) (charToByte(next.charAt(2)) << 4 | charToByte(next.charAt(3)));
                    d[start++] = b;
                    mode16 = true;
                    i += 3;
                }
            }
            if (!mode16) {
                if(start>0) {
                    byte[] ddd = new byte[start];
                    System.arraycopy(d, 0, ddd, 0, start);
                    result.append(new String(ddd, encoding));
                    start=0;
                }
                result.append(c);
            }
        }
        if(start>0) {
            byte[] ddd = new byte[start];
            System.arraycopy(d, 0, ddd, 0, start);
            result.append(new String(ddd, encoding));
        }
        return  result.toString();
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(Character.toUpperCase(c));
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = hexToString("[{\"action\":\"startUp\",\"appVersion\":\"5.2.5\",\"deviceId\":\"94e669c1-b4b4-4c8c-b1ce-787bae0ef7e0\",\"duration\":0,\"isFirst\":0,\"netWorkState\":\"WIFI\",\"phoneBrand\":\"Nokia TA-1000\",\"phoneType\":\"Android\",\"siteId\":181,\"startTime\":\"2019-01-07 07:09:01\",\"systemVersion\":\"8.1.0\"},{\"action\":\"visitPage\",\"appVersion\":\"5.2.5\",\"deviceId\":\"94e669c1-b4b4-4c8c-b1ce-787bae0ef7e0\",\"duration\":13394,\"isFirst\":0,\"netWorkState\":\"WIFI\",\"nowScene\":\"home\",\"phoneBrand\":\"Nokia TA-1000\",\"phoneType\":\"Android\",\"siteId\":181,\"startTime\":\"2019-01-07 07:09:19\",\"systemVersion\":\"8.1.0\",\"userName\":\"15822263635\"},{\"action\":\"flowScene\",\"appVersion\":\"5.2.5\",\"deviceId\":\"94e669c1-b4b4-4c8c-b1ce-787bae0ef7e0\",\"duration\":0,\"isFirst\":0,\"netWorkState\":\"WIFI\",\"nowScene\":\"home\",\"phoneBrand\":\"Nokia TA-1000\",\"phoneType\":\"Android\",\"siteId\":181,\"startTime\":\"2019-01-07 07:09:27\",\"systemVersion\":\"8.1.0\",\"toScene\":\"shareQRcode\",\"userName\":\"15822263635\"},{\"action\":\"share\",\"appVersion\":\"5.2.5\",\"deviceId\":\"94e669c1-b4b4-4c8c-b1ce-787bae0ef7e0\",\"duration\":0,\"isFirst\":0,\"netWorkState\":\"WIFI\",\"phoneBrand\":\"Nokia TA-1000\",\"phoneType\":\"Android\",\"proId\":\"15822263635\",\"siteId\":181,\"startTime\":\"2019-01-07 07:09:34\",\"systemVersion\":\"8.1.0\",\"title\":\"\\xE8\\xBA\\xAB\\xE8\\xBE\\xB9\\xE4\\xBA\\xBA\\xEF\\xBC\\x8C\\xE8\\xBA\\xAB\\xE8\\xBE\\xB9\\xE4\\xBA\\x8B\\xEF\\xBC\\x8C\\xE6\\x9C\\xAC\\xE5\\x9C\\xB0\\xE4\\xBA\\xBA\\xE8\\x87\\xAA\\xE5\\xB7\\xB1\\xE7\\x9A\\x84\\xE6\\x89\\x8B\\xE6\\x9C\\xBAAPP!\",\"type\":\"21\",\"userName\":\"15822263635\"}]", "UTF-8");
        System.out.println(str);
    }
}
